package com.yx.changdao.web.controller;

import com.yx.changdao.common.annotation.AutoLog;
import com.yx.changdao.common.annotation.ShiroDesc;
import com.yx.changdao.common.data.Result;
import com.yx.changdao.common.exception.AlertException;
import com.yx.changdao.common.vo.RolePermissionSetVo;
import com.yx.changdao.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Description: 权限设置
 * @Auther: zhangcongzhen
 * @create 2020/4/17
 */
@RestController
@RequestMapping(value = "/permission/set")
@Validated
public class SysPermissionSetController {
    @Autowired
    SysPermissionSetService sysPermissionSetService;
    @Autowired
    SysRolePermissionService sysRolePermissionService;
    @Autowired
    SysMenuService sysMenuService;
    @Autowired
    SysRoleService sysRoleService;
    @Autowired
    SysUserRoleService sysUserRoleService;

    /**
     * 权限设置-角色列表
     */
    @AutoLog
    @GetMapping({"/list"})
    @RequiresPermissions("permission:set:list")
    @ShiroDesc("权限设置:列表")
    public Result list(@RequestParam("page") Long page,
                       @RequestParam(value = "limit", required = false) Integer limit) {
        return sysPermissionSetService.getResult(page, limit);
    }

    private void check(RolePermissionSetVo rolePermissionSetVo) throws AlertException {

        List<String> permIds = rolePermissionSetVo.getPermIds();

        if(!permIds.contains("problem:home:execute") && !permIds.contains("problem:home:leader")){
            throw new AlertException("必须选择一个首页");
        }

        if(permIds.contains("problem:home:execute") && permIds.contains("problem:home:leader")){
            throw new AlertException("执行首页和总控首页不能同时拥有");
        }

        if(permIds.contains("dep:contact:group:zkbm:list") && permIds.contains("dep:contact:group:list")){
            throw new AlertException("总控联户组管理和执行联户组不能同时拥有");
        }

        if(permIds.contains("community:township:list") && permIds.contains("community:execute:list")){
            throw new AlertException("总控房屋管理和执行房屋管理不能同时拥有");
        }
    }

    /**
     * 权限设置-新增角色，同时给角色添加权限
     * @return Result
     */
    @AutoLog
    @PostMapping
    /*@RequiresPermissions("permission:set:add")
    @ShiroDesc("权限设置:添加")*/
    @Transactional(rollbackFor = Exception.class)
    public Result add(@RequestBody @Valid RolePermissionSetVo rolePermissionSetVo) throws AlertException {

        check(rolePermissionSetVo);

        return sysPermissionSetService.add(rolePermissionSetVo);
    }

    /**
     * 根据角色id获取角色信息以及对应的权限id的集合
     * @param roleId 角色id
     * @return Result
     */
    @AutoLog
    @GetMapping("/role/{roleId}")
    /*@RequiresPermissions("permission:set:look")
    @ShiroDesc("系统角色:查看")*/
    public Result getRolePermission(@PathVariable("roleId") String roleId) throws AlertException {
        return sysPermissionSetService.getRolePermission(roleId);
    }

    /**
     * 修改
     * @return
     * @throws AlertException
     */
    @AutoLog
    @PutMapping
    /*@RequiresPermissions("permission:set:update")
    @ShiroDesc("系统角色:更新保存")*/
    public Result update(@RequestBody @Valid RolePermissionSetVo rolePermissionSetVo) throws AlertException {

        check(rolePermissionSetVo);

        return sysPermissionSetService.update(rolePermissionSetVo);
    }

    /**
     * 删除角色
     *
     * @param roleId 待删除角色id
     * @return Result
     */
    @AutoLog
    @DeleteMapping
    /*@RequiresPermissions("permission:set:del")
    @ShiroDesc("系统设置:删除")*/
    public Result delete(@RequestParam("roleId") String roleId) throws AlertException {
        return sysPermissionSetService.delete(roleId);
    }


}
